<?php

namespace App\Models;

use App\Service\Base\Model;
use Illuminate\Support\Facades\DB;


class UserAgent extends Model
{

    // 用户管理
    protected $table = 'user_agent';

    protected $cacheFields = ['id', 'uid', 'verify'];

    //  {"id":"自编号","uid":"用户ID","agent_type":"代理类型","agent_name":"代理名称","city":"城市","address":"所在地","description":"代理描述","license_img":"营业执照","school_allow":"办学许可","form_time":"开始时间","end_time":"代理结束时间（代理时间为一年）","is_pay":"是否支付（1-是）","verify":"是否审核（1-是）","ctime":"注册时间","mtime":"更新时间"}


    //  array ( 'id' => '$this->getData("id", null, "trim")', 'uid' => '$this->getData("uid", null, "trim")', 'agent_type' => '$this->getData("agent_type", null, "trim")', 'agent_name' => '$this->getData("agent_name", null, "trim")', 'city' => '$this->getData("city", null, "trim")', 'address' => '$this->getData("address", null, "trim")', 'description' => '$this->getData("description", null, "trim")', 'license_img' => '$this->getData("license_img", null, "trim")', 'school_allow' => '$this->getData("school_allow", null, "trim")', 'form_time' => '$this->getData("form_time", null, "trim")', 'end_time' => '$this->getData("end_time", null, "trim")', 'is_pay' => '$this->getData("is_pay", null, "trim")', 'verify' => '$this->getData("verify", null, "trim")', 'ctime' => '$this->getData("ctime", null, "trim")', 'mtime' => '$this->getData("mtime", null, "trim")', )


    // $id = $this->getData("id", null, 'trim'); $uid = $this->getData("uid", null, 'trim'); $agent_type = $this->getData("agent_type", null, 'trim'); $agent_name = $this->getData("agent_name", null, 'trim'); $city = $this->getData("city", null, 'trim'); $address = $this->getData("address", null, 'trim'); $description = $this->getData("description", null, 'trim'); $license_img = $this->getData("license_img", null, 'trim'); $school_allow = $this->getData("school_allow", null, 'trim'); $form_time = $this->getData("form_time", null, 'trim'); $end_time = $this->getData("end_time", null, 'trim'); $is_pay = $this->getData("is_pay", null, 'trim'); $verify = $this->getData("verify", null, 'trim'); $ctime = $this->getData("ctime", null, 'trim'); $mtime = $this->getData("mtime", null, 'trim');

    // 代理类型
    public function getType($getKey = true)
    {
        //学校school，机构organ
        $channels = [
            // 其他  暂时没有用的
            'school' => '学校',
            'organ'  => '机构',
        ];

        if(empty($getKey)){
            return $channels;
        }

        return array_keys($channels);
    }

    // 代理操作
    public function onAgent($uid, $data)
    {
        // 判断是否已经代理
        $payDb  = new UserPay();
        $userDb = new User();
        $user   = ['agent_name' => $data['agent_name'], 'agent_type' => $data['agent_type'], 'is_agent' => 1, 'verify_agent' => 0,];
        $user   = ['is_agent' => 1, 'verify_agent' => 0,];
        //$_end   = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y") + 1);
        // 'agent_time' => LARAVEL_TIME, 'end_time' => $_end

        // 判断是否已经代理过了
        $info = $this->where('uid', $uid)->first();
        if($info){
            $info->fill($data);
        } else{
            $this->fill($data);
        }


        // 保存信息
        DB::beginTransaction();
        try{
            if($info){
                $result = $info->save();
                // 改变用户信息
                $result = $userDb->where(['id' => $uid])->update($user);
            } else{
                $result = $this->save();
                // 改变用户信息
                $user['agent_id'] = $this->id;
                // 改变用户信息
                $result = $userDb->where(['id' => $uid])->update($user);
            }

            // 支付记录

            DB::commit();
        } catch(\Exception $e){
            DB::rollBack();

            return $this->return_false($e->getMessage());
        }

        $user['id'] = $uid;
        $userDb->deleteCacheShow($user);
        $this->deleteCacheShow($data, $info);

        return true;
    }

    // 添加（后台添加）
    public function oneAdd($data = [])
    {
        $uid = $data['uid'] ?? 0;
        if(empty($uid)){
            return $this->return_false('请指定代理用户！');
        }

        // 判断是否已经代理
        $userDb = new User();
        $user   = ['agent_name' => $data['agent_name'], 'agent_type' => $data['agent_type'], 'is_agent' => 1, 'verify' => $data['verify'] ?? 0, 'agent_time' => LARAVEL_TIME];

        // 保存信息
        DB::beginTransaction();
        try{
            // 改变用户信息
            $result = $userDb->where(['id' => $uid])->update($user);
            //$result = $this->where(['uid' => $uid])->update($data);

            $this->fill($data);
            $result = $this->save();

            DB::commit();
        } catch(\Exception $e){
            DB::rollBack();

            return $this->return_false($e->getMessage());
        }

        $user['id'] = $uid;
        $userDb->deleteCacheShow($user);
        $this->deleteCacheShow($data);

        return true;
    }

    // 审核操作
    public function onVerify($id, $verify)
    {
        if(empty($id)){
            return $this->return_false('请指定审核记录！');
        }
        if(empty($verify)){
            return $this->return_false('请指定审核状态！');
        }

        // 判断是否已经代理
        $info = $this->isCache(true)->oneFind(['id' => $id]);
        if(empty($info)){
            return $this->return_false('该条代理信息不存在！');
        }
        if($info['verify'] < 0){
            return $this->return_false('该条代理信息已经被删除！');
        }
        if($info['verify'] > 0){
            return $this->return_false('该条代理信息已经被审核！');
        }

        // 更改信息
        $userDb = new User();
        $_end   = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y") + 1);
        $user   = ['verify' => $verify, 'end_time' => $_end];
        $data   = ['verify_agent' => $verify, 'end_time' => $_end];


        // 保存信息
        DB::beginTransaction();
        try{
            // 改变用户信息
            $result = $userDb->where(['agent_id' => $id])->update($user);

            $result = $this->where(['id' => $id])->update($data);

            DB::commit();
        } catch(\Exception $e){
            DB::rollBack();

            return $this->return_false($e->getMessage());
        }

        $user['agent_id'] = $id;
        $data['id']       = $id;
        $userDb->deleteCacheShow($user);
        $this->deleteCacheShow($data);

        return true;
    }

    // 定义验证操作
    public function rules()
    {
        $add = [
        ];

        $edit = [
        ];

        $info = [
        ];


        $rule = [
            'uid'          => 'required',
            'agent_type'   => 'required',
            'agent_name'   => 'required',
            'city'         => 'required',
            'address'      => 'required',
            'description'  => 'required',
            'license_img'  => 'required',
            'school_allow' => 'required',
            //'form_time'    => 'required',
            //'end_time'     => 'required',
            'is_pay'       => 'in:0,1',
            'is_propose'   => 'in:0,1',
            'verify'       => 'in:0,1,2',
        ];


        $msg = [
            'id'           => '自编号',
            'uid'          => '用户ID',
            'agent_type'   => '代理类型',
            'agent_name'   => '代理名称',
            'city'         => '城市',
            'address'      => '所在地',
            'description'  => '代理描述',
            'license_img'  => '营业执照',
            'school_allow' => '办学许可',
            'form_time'    => '开始时间',
            'end_time'     => '代理结束时间（代理时间为一年）',
            'is_pay'       => '是否支付（1-是）',
            'verify'       => '是否审核（1-是）',
            'is_propose'   => '是否推荐（1-是）',
            'ctime'        => '注册时间',
            'mtime'        => '更新时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add, 'info' => $info];
    }

}
